//
//  SISSUploadImportantMattersNotiViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/4.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSUploadImportantMattersNotiViewController.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSHouseProgressModel.h"
#import "SISSHouseProgressRecordInfoModel.h"
#import "SISSPassengerProgressModel.h"
#import "SISSPassengerProgressRecordInfoModel.h"
#import "SISSSellerImportantNotiModel.h"
#import "SISSBuyerImportantNotiModel.h"

@interface SISSUploadImportantMattersNotiViewController ()

@end

@implementation SISSUploadImportantMattersNotiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.isSellerNoti ? @"二手房交易卖方重要事项告知书" : @"二手房交易买方重要事项告知书";
    self.uploadImageView.titleStr = self.isSellerNoti ? @"卖方重要事项告知书电子档图片" : @"买方重要事项告知书电子档图片";
    self.tipStr = self.isSellerNoti ? @"您和卖方尚未签署买方重要事项告知书请及时签署，保障各自权益。" : @"您和买方尚未签署买方重要事项告知书请及时签署，保障各自权益。";
    
    if (self.sellerImportantNotiModel) {
        self.tipStr = @"";
        self.uploadImageView.imageUrlArr = self.sellerImportantNotiModel.image.mutableCopy;
        self.remarkView.rightTextField.text = self.sellerImportantNotiModel.otherdesc;
    }
    
    if (self.buyerImportantNotiModel) {
        self.tipStr = @"";
        self.uploadImageView.imageUrlArr = self.buyerImportantNotiModel.image.mutableCopy;
        self.remarkView.rightTextField.text = self.buyerImportantNotiModel.otherdesc;
    }
}


- (void)confirmButtonClickAction {
    //    [super confirmButtonClickAction];
    [self.view endEditing:YES];
    
    
    NSString *ahbpId = self.houseProgressModel ? self.houseProgressModel.ahbpId : self.passengerProgressRecordInfoModel.ahbpId;
    NSString *adbpId = self.passengerProgressModel ? self.passengerProgressModel.adbpId : self.houseProgressRecordInfoModel.adbpId;
    
    NSString *otherdesc = self.remarkView.rightTextField.text ? self.remarkView.rightTextField.text : @"";
    if (self.isSellerNoti) {
        if (!self.sellerImportantNotiModel && (!self.uploadImageView.images || self.uploadImageView.images.count == 0)) {
            [SISSTools showSVWithTitle:self.isSellerNoti ? @"请选择卖方重要事项告知书电子档图片" : @"请选择买方重要事项告知书电子档图片"];
            return;
        }
        NSInteger houseType = self.houseProgressModel ? 1 : self.passengerProgressRecordInfoModel.tradeBusinessType;
        if (houseType == 2) ahbpId = self.passengerProgressRecordInfoModel.ahrId;
        
        if (self.sellerImportantNotiModel) { // 修改
           
            NSDictionary *parameters = @{
                                         @"id" : self.sellerImportantNotiModel.ID,
                                         @"ahbpId" : ahbpId,
                                         @"houseType" : @(houseType),
                                         @"otherdesc" : otherdesc
                                         };
            
            [kNetworkManager updateProgressSellerImportantNotificationWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType: self.houseProgressModel ? SISSProgressPopBackClassTypeHouse : SISSProgressPopBackClassTypePassenger];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
            
        } else { // 新增
            NSDictionary *parameters = @{
                                         @"ahbpId" : ahbpId,
                                         @"adbpId" : adbpId,
                                         @"house_type" : @(houseType),
                                         @"otherdesc" : otherdesc
                                         };
            [kNetworkManager insertProgressSellerImportantNotificationWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType: self.houseProgressModel ? SISSProgressPopBackClassTypeHouse : SISSProgressPopBackClassTypePassenger];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    } else {
        if (!self.buyerImportantNotiModel && (!self.uploadImageView.images || self.uploadImageView.images.count == 0)) {
            [SISSTools showSVWithTitle:self.isSellerNoti ? @"请选择卖方重要事项告知书电子档图片" : @"请选择买方重要事项告知书电子档图片"];
            return;
        }
        NSInteger demandType = self.passengerProgressModel ? 1 : self.houseProgressRecordInfoModel.tradeBusinessType;
        if (demandType == 2) adbpId = self.houseProgressRecordInfoModel.abdId;
        
        if (self.buyerImportantNotiModel) { // 修改
            
            NSDictionary *parameters = @{
                                         @"id" : self.buyerImportantNotiModel.ID,
                                         @"adbpId" : adbpId,
                                         @"demandType" : @(demandType),
                                         @"otherdesc" : otherdesc
                                         };
            
            [kNetworkManager updateProgressBuyerImportantNotificationWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType: self.houseProgressModel ? SISSProgressPopBackClassTypeHouse : SISSProgressPopBackClassTypePassenger];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        } else { // 新增
            NSDictionary *parameters = @{
                                         @"ahbpId" : ahbpId,
                                         @"adbpId" : adbpId,
                                         @"demand_type" : @(demandType),
                                         @"otherdesc" : otherdesc
                                         };
            [kNetworkManager insertProgressBuyerImportantNotificationWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType: self.houseProgressModel ? SISSProgressPopBackClassTypeHouse : SISSProgressPopBackClassTypePassenger];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    }
    
}



@end
